<?php

namespace app\admins\controller;

use think\Controller;
use think\Db;
use Util\SysDb;

class Role extends Controller
{
    public function roleList()
    {
        $gid = input('param.gid');
        $val = trim(input('param.key'));
        $page = input('param.page');
        $limit = input('param.limit');
        $this->db = new SysDb();

        //模糊查询
        if ($val == '') {
            $data = $this->db->table('admins')->where(['gid' => $gid])->lists();
            $count = $this->db->table('admins')->where(['gid' => $gid])->count();
            if ($data) {
                //分页
                $tol = ($page - 1) * $limit;

                $list = db('admins')->where(['gid' => $gid])->limit($tol, $limit)->select();
                $index['code'] = 0;
                $index['count'] = $count;
                $list1 = [];
                foreach ($list as $l) {
                    if ($l['status'] == 0) {
                        switch ($l['gid']) {
                            case 1:
                                $l['role'] = '管理员';
                                break;
                            case 2:
                                $l['role'] = '学生';
                                break;
                            case 3:
                                $l['role'] = '教师';
                                break;
                        }
                    }
                    if ($l['status'] == 1) {
                        $l['role'] = '已禁用';
                    }
                    array_push($list1, $l);
                }

                $index['data'] = $list1;
                exit(json_encode($index));
            } else {
                $index['code'] = 2;
                $index['msg'] = '暂无记录';
                exit(json_encode($index));
            }
        } else {
            //分页
            $count = $this->db->table('admins')->where(['gid' => $gid])->count();
            $tol = ($page - 1) * $limit;
            $list1 = [];

            $list = DB::name('admins')->where(['gid' => $gid])->where('username', 'like', '%'.$val.'%')->limit($tol, $limit)->select();
            if (!$list) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
                exit(json_encode($index));
            }
            foreach ($list as $l) {
                if ($l['status'] == 0) {
                    switch ($l['gid']) {
                        case 1:
                            $l['role'] = '管理员';
                            break;
                        case 2:
                            $l['role'] = '学生';
                            break;
                        case 3:
                            $l['role'] = '教师';
                            break;
                    }
                }
                if ($l['status'] == 1) {
                    $l['role'] = '已禁用';
                }
                array_push($list1, $l);
            }
            $index['count'] = $count;
            $index['data'] = $list1;
            $index['code'] = 0;
            exit(json_encode($index));
        }
    }

    public function roleChange()
    {
        $id = input('param.id');
        $gid = input('param.gid');
        $status = input('param.status');
        $this->db = new SysDb();
        if ($status == 0) {
            $data = $this->db->table('admins')->where(['id' => $id])->update(['gid' => $gid, 'status' => $status]);
            if ($data) {
                $index['code'] = 0;
                $index['msg'] = '修改成功';
                exit(json_encode($index));
            } else {
                $index['code'] = 2;
                $index['msg'] = '修改失败';
                exit(json_encode($index));
            }
        }
        if ($status == 1) {
            $data = $this->db->table('admins')->where(['id' => $id])->update(['status' => $status]);
            if ($data) {
                $index['code'] = 0;
                $index['msg'] = '修改成功';
                exit(json_encode($index));
            } else {
                $index['code'] = 2;
                $index['msg'] = '修改失败';
                exit(json_encode($index));
            }
        }
    }
}
